package root.action;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import root.Interceptor.UserInterceptor;
import root.service.MenuService;
import root.utils.StringHelper;
import root.utils.WebConstants;

import java.util.Date;
import java.util.List;

/**
 * 菜单管理
 * @author 吕梁山
 * @date 2018/9/25
 */
@Before(UserInterceptor.class)
public class MenuAction extends Controller {

    private MenuService menuService = new MenuService();

    /**
     * 分页获取菜单
     */
    public void index(){
        JSONObject jsonObject = new JSONObject();
        String name = getPara("name");
        Integer pageNumber = getParaToInt("cupage", 1);
        Page<Record> page = menuService.getMenuPage(name, pageNumber, 15);
        jsonObject.put("code", 0);
        jsonObject.put("page", page);
        renderJson(jsonObject);
    }

    public void getMenuList(){
        JSONObject jsonObject = new JSONObject();
        List<Record> menu = menuService.getMenuList();
        jsonObject.put("code",0);
        jsonObject.put("list",menu);
        renderJson(jsonObject);
    }

    /**
     * 获取一级菜单
     */
    public void getFirstMenu(){
        JSONObject jsonObject = new JSONObject();
        List<Record> menu = menuService.getFirstMenu();
        jsonObject.put("code",0);
        jsonObject.put("list",menu);
        renderJson(jsonObject);
    }

    /**
     * 添加菜单
     */
    public void addMenu(){
        JSONObject jsonObject = new JSONObject();
        String menuId = getPara("menuId");
        String menuName = getPara("menuName");
        String menuIcon = getPara("menuIcon");
        String menuUrl = getPara("menuUrl");
        String menuLabel = getPara("menuLabel");
        int parentMenu = getParaToInt("parentMenu",1);
        Record menu = new Record();
        menu.set("name",menuName);
        menu.set("icon",menuIcon);
        menu.set("url",menuUrl);
        menu.set("parent_id",parentMenu);
        menu.set("label",menuLabel);
        if(StringHelper.isNotEmpty(menuId)){
            menu.set("id", menuId);
            menuService.updateMenu(menu);
        } else {
            Record maxRecord = menuService.getMaxIdByParentId(parentMenu);
            int maxId = parentMenu;
            if(maxRecord!=null){
                maxId = maxRecord.getInt("id");
            }
            if(parentMenu == 1){
                menu.set("id", maxId + 1000);
            } else {
                menu.set("id", maxId + 1);
            }
            menu.set("create_by",getUserId());
            menu.set("create_date",new Date());
            menuService.saveMenu(menu);
        }
        jsonObject.put("code",0);
        renderJson(jsonObject);
    }

    /**
     * 获取单个菜单详情
     */
    public void getMenu(){
        JSONObject jsonObject = new JSONObject();
        String id = getPara("id");
        Record menu = menuService.getMenu(id);
        jsonObject.put("code", 0);
        jsonObject.put("data", menu);
        renderJson(jsonObject);
    }

    /**
     * 删除单个菜单
     */
    public void deleteMenu(){
        JSONObject jsonObject = new JSONObject();
        String id = getPara("id");
        List<Record> childrenList = menuService.getChildrenList(id);
        if(childrenList.size()>0){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "存在子菜单,无法删除");
        } else{
            menuService.deleteMenu(id);
            jsonObject.put("code", 0);
        }
        renderJson(jsonObject);
    }

    private String getUserId(){
        return getCookie(WebConstants.USERID);
    }
}
